package com.wulis.provider.basic.web;

import static com.wulis.common.model.enums.VersionEnum.WEB_V_1_0;

import java.util.List;

import javax.validation.Valid;

import com.wulis.provider.basic.model.vo.InsertDepartmentVo;
import com.wulis.provider.basic.service.DepartmentService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import com.wulis.common.annotation.ApiVersion;
import com.wulis.common.model.dto.IdDto;
import com.wulis.common.support.BaseController;
import com.wulis.common.wrapper.ApiWrapMapper;
import com.wulis.common.wrapper.ApiWrapper;
import com.wulis.provider.basic.model.dto.InsertDepartmentDto;
import com.wulis.provider.basic.model.dto.QueryDepartmentTreeDto;
import com.wulis.provider.basic.model.dto.UpdateDepartmentDto;
import com.wulis.provider.basic.model.vo.QueryDepartmentTreeVo;
import com.wulis.provider.basic.model.vo.QueryDepartmentVo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * @author WuliBao
 * @date 2020-05-21
 */
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("web/basic/department")
@Api(tags = "基础信息管理:部门管理")
public class DepartmentController extends BaseController {
    private final DepartmentService departmentService;
    
    @ApiVersion(WEB_V_1_0)
    @GetMapping("queryDepartmentTree")
    @ApiOperation("查询部门信息列表(树形)")
    @PreAuthorize("@api.hasPermission('web:basic:department:queryDepartmentTree')")
    public ApiWrapper<List<QueryDepartmentTreeVo>> queryDepartmentTree(@Valid QueryDepartmentTreeDto queryDepartmentTreeDto) {
        return ApiWrapMapper.success(departmentService.queryDepartmentTree(queryDepartmentTreeDto));
    }
    
    @ApiVersion(WEB_V_1_0)
    @GetMapping("queryDepartment")
    @ApiOperation("查询部门信息")
    @PreAuthorize("@api.hasPermission('web:basic:department:queryDepartment')")
    public ApiWrapper<QueryDepartmentVo> queryDepartment(@Valid IdDto idDto) {
        return ApiWrapMapper.success(departmentService.queryDepartment(idDto.getId()));
    }
    
    @ApiVersion(WEB_V_1_0)
    @PostMapping("insertDepartment")
    @ApiOperation("新增部门信息")
    @PreAuthorize("@api.hasPermission('web:basic:department:insertDepartment')")
    public ApiWrapper<InsertDepartmentVo> insertDepartment(@Valid @RequestBody InsertDepartmentDto insertDepartmentDto) {
        return ApiWrapMapper.success(departmentService.insertDepartment(insertDepartmentDto));
    }
    
    @ApiVersion(WEB_V_1_0)
    @PutMapping("updateDepartment")
    @ApiOperation("修改部门信息")
    @PreAuthorize("@api.hasPermission('web:basic:department:updateDepartment')")
    public ApiWrapper<Integer> updateDepartment(@Valid @RequestBody UpdateDepartmentDto updateDepartmentDto) {
        return ApiWrapMapper.success(departmentService.updateDepartment(updateDepartmentDto));
    }
    
    @ApiVersion(WEB_V_1_0)
    @DeleteMapping("deleteDepartment")
    @ApiOperation("删除部门信息")
    @PreAuthorize("@api.hasPermission('web:basic:department:deleteDepartment')")
    public ApiWrapper<Integer> deleteDepartment(@Valid @RequestBody IdDto idDto) {
        return ApiWrapMapper.success(departmentService.deleteDepartment(idDto.getId()));
    }
}
